package com.ruoyi.web.controller.PhoneLogin;


import com.ruoyi.system.service.PhoneLoginService;
import com.ruoyi.system.weixin.HttpClientUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@EnableScheduling
public class TimerThread {

    @Autowired
    private PhoneLoginService phoneLoginService;
    @Autowired
    private PhoneLoginController phoneLoginController;
    /**
     * @Scheduled(fixedRate = 5*1000)
     * 每天11点定时执行任务
     */
    @Scheduled(cron="0 0 11 * * ?")
    @Transactional
    public void fixedRateTest(){
        List<Map<String, Object>> list =phoneLoginService.CallShellProcedure("R2006003");
        for (int i=0;i<list.size();i++){
            if(list.get(i).get("qds").equals("0")){
                try {
                    fxx_now(list.get(i).get("id").toString(),"你今天还未签到，及时签到可以获得金币优惠券，心愿卡等海量奖励");
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }
        List<Map<String, Object>> lists =phoneLoginService.CallShellProcedure("R2020006");
        String pd=lists.get(0).get("codes").toString();
        if (pd.equals("0")){
            JSONObject jsonObject =JSONObject.fromObject(HttpClientUtil.doPost("https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=y6XtLuG84rGqOlfvKKBEPb2Q&client_secret=8IhiXe4sDCbPCYKoAMG2ti0aedcQLysS"));
            phoneLoginService.CallShellProcedure("R2020007|"+jsonObject.get("access_token"));
        }
    }


    /**
     * @Scheduled(cron = "0/180 * * * * ?")
     * 每次任务执行完，等待5s后再次执行
     */

    /**
     * @Scheduled(fixedRate = 5*1000)
     * 每天11点定时执行任务
     */
    @Scheduled(cron="0 5 0 * * ?")
    @Transactional
    public void fixedRateTest2(){
        List<Map<String, Object>> list =phoneLoginService.CallShellProcedure("R2021001");

    }

    @Scheduled(cron = "0 0/3 * * * ?")
    public void cornTest(){
        List<Map<String, Object>> list =phoneLoginService.CallShellProcedure("R2006007");
        phoneLoginService.CallShellProcedure("R2015005");
    }

    /**微信提醒
     */

    public String fxx_now(String idss,String nr) throws ParseException {
        String mb="qruxG09efxaplOGv2DFWvY0_N547IzAFALP9K3O8peg";
        String ps="R2012009|"+idss;
        List<Map<String, Object>> list =  phoneLoginService.CallShellProcedure(ps);
        String openids=list.get(0).get("openid").toString();
        String ps2="R2013002|"+idss;
        String  formid=phoneLoginService.CallShellProcedure(ps2).get(0).get("codes").toString();
        Map<String, TemplateData> map = new HashMap<>();
        map.put("keyword1", new TemplateData("校园帮帮签到辅助"));
        map.put("keyword2", new TemplateData(getBeforeByHourTime(0)));
        map.put("keyword3", new TemplateData(nr));
        com.alibaba.fastjson.JSONObject js =phoneLoginController.sendTemplateMessage(openids,mb , "pages/index/index", formid, map);
        return "";
    }

    public String getBeforeByHourTime(int ihour){
        String returnstr = "";
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - ihour);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        returnstr = df.format(calendar.getTime());
        return returnstr;
    }

}
